import {specslist,specscount} from "../../request/api.js"
const state ={
list:[],
total:0,
size:2,
page:1
}
const mutations ={
    changeList(state,arr){
        state.list=arr
    },
    changeTotal(state,num){
        state.total=num
    },
    changePage(state,num){
        state.page=num
    }
}

const actions={
    asyncChangeList(context,flag){
        let reqObj =flag?{}:{page:context.state.page,size:context.state.size}
        specslist(reqObj).then(res=>{
            let arr = res.data.list?res.data.list:[]
if(arr.length<=0 &&context.state.page>1){ 
    context.commit("changePage",context.state.page-1)
    context.dispatch("asyncChangeList")
}

            context.commit("changeList",res.data.list)

            arr.forEach(item=>{
                item.attrs = JSON.parse(item.attrs)
              })
               context.commit("changeList",arr)
        })
    },
    asyncChangeTotal(context){
        specscount().then(res=>{
 context.commit("changeTotal",res.data.list[0].total)
        })   
    },
    asyncChangePage(context,num){
        context.commit("changePage",num)
        context.dispatch("asyncChangeList")
    }
}

const getters={
    list(state){
return state.list
    },
    total(state){
        return state.total
    },
    size(state){
        return state.size
    }
   
}

export default{
    state,mutations,actions,getters,namespaced:true
}